import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getHotPlaylistApi, getAllSongListCatApi, getSongListIdBycatApi } from '../../../api'
import { playListType } from './type'

export const useRadarStore = defineStore('radarStore', () => {
    const allSongListCatList = ref<playListType[]>([])
    const categories = ref([] as { name: string, catIndex: number }[])
    const songListByCat = ref([] as any[])
    const songListId = ref(0 as number)
    const getHotPlaylistCatlist = async () => {
        let res = await getHotPlaylistApi()
        songListByCat.value = res.tags
    }
    const getAllSongListCatList = async () => {
        let res = await getAllSongListCatApi()
        allSongListCatList.value = res.sub

        // 使用 Object.values() 遍历对象值
        categories.value = Object.values(res.categories).map((item: any, index: number) => ({
            name: item,
            catIndex: index
        }))
    }
    // 获取歌单分类
    const getSongListByCat = async (cat: number) => {
        if (cat === -1) {
            await getHotPlaylistCatlist()
            return
        }
        songListByCat.value = allSongListCatList.value.filter(item => item.category === cat)
    }
    // 获取歌单
    const getSongListIdByCat = async (cat: string) => {
        let res: any = await getSongListIdBycatApi(cat)
        songListId.value = res.playlists[0].id
        console.log(songListId.value);

    }
    return {
        allSongListCatList,
        categories,
        songListByCat,
        songListId,
        getHotPlaylistCatlist,
        getAllSongListCatList,
        getSongListByCat,
        getSongListIdByCat
    }
})
